package com.ruoyi.web.controller.dunningfrom;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.DunningFrom;
import com.ruoyi.domain.ProApplicationPayment;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IDunningFromService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目总监催款Controller
 *
 * @author ruoyi
 * @date 2020-07-09
 */
@Controller
@RequestMapping("/ruoyi/fromVerify")
public class DunningFromVerController extends BaseController {
    private String prefix = "ruoyi/from";

    @Autowired
    private IDunningFromService dunningFromService;

    @RequiresPermissions("ruoyi:fromVerify:view")
    @GetMapping()
    public String from() {
        return prefix + "/fromVerify";
    }

    /**
     * 查询催款申请单列表
     */
    @RequiresPermissions("ruoyi:fromVerify:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Long pId) {
        startPage();
        List<DunningFrom> list = dunningFromService.selectDunningFromListss(pId);
        return getDataTable(list);
    }

    /**
     * 导出催款申请单列表
     */
    @RequiresPermissions("ruoyi:from:export")
    @Log(title = "催款申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DunningFrom dunningFrom) {
        List<DunningFrom> list = dunningFromService.selectDunningFromList(dunningFrom);
        ExcelUtil<DunningFrom> util = new ExcelUtil<DunningFrom>(DunningFrom.class);
        return util.exportExcel(list, "from");
    }

    /**
     * 新增催款申请单
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存催款申请单
     */
    @RequiresPermissions("ruoyi:from:add")
    @Log(title = "催款申请单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DunningFrom dunningFrom,@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        dunningFrom.setdFile(fileName);
        String name = ShiroUtils.getLoginName();
        dunningFrom.setdProposer(name);
        return toAjax(dunningFromService.insertDunningFrom(dunningFrom));
    }

    /**
     * 修改保存催款申请单
     */
    @RequiresPermissions("ruoyi:from:edit")
    @Log(title = "催款申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DunningFrom dunningFrom){
        return toAjax(dunningFromService.updateDunningFrom(dunningFrom));
    }

    /**
     * 删除催款申请单
     */
    @RequiresPermissions("ruoyi:from:remove")
    @Log(title = "催款申请单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dunningFromService.deleteDunningFromByIds(ids));
    }

    /**
     * 未付款提交到行政
     */
    @Log(title = "行政", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long dId) {
        dunningFromService.conExaStatus(dId);
        return success();
    }

    /**
     * 已付款
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/over")
    @ResponseBody
    public AjaxResult over(Long dId) {
        dunningFromService.overExaStatus(dId);
        return success();
    }

}
